<?php
class Item
{
    private $name;
    private $slot;
    private $buyer;
    private $wowid;
    private $drop;
    private $heroic;

    public function __construct($name, $buyer, $wowid)
    {
        $this->name = $name;
        $this->buyer = $buyer;
        $this->wowid = $wowid;

        $content = file_get_contents(localURL($wowid));
        if(@$xmlDoc = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA))
        {
            $this->slot = $this->parseSlot($xmlDoc);

            $json_a = tojson($xmlDoc);

            $this->drop = $this->parseDrop($json_a);
            $this->heroic = $this->parseHeroic($json_a);
        }
        else
        {
            $this->slot = "-1";
            $this->drop = "Unknown";
            $this->heroic = "false";
        }
    }

    public function getHeroic()
    {
        return $this->heroic;
    }

    public function setHeroic($heroic)
    {
        $this->heroic = $heroic;
    }

    public function display()
    {
        return $this->name . " - " . $this->slot . " - " . $this->drop . " - " . $this->buyer . " - " . $this->wowid . " - " . $this->heroic;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getSlot()
    {
        return $this->slot;
    }

    public function setSlot($slot)
    {
        $this->slot = $slot;
    }

    public function getBuyer()
    {
        return $this->buyer;
    }

    public function setBuyer($buyer)
    {
        $this->buyer = $buyer;
    }

    public function getWowid()
    {
        return $this->wowid;
    }

    public function setWowid($wowid)
    {
        $this->wowid = $wowid;
    }

    public function getDrop()
    {
        return $this->drop;
    }

    public function setDrop($drop)
    {
        $this->drop = $drop;
    }

    private function parseHeroic(&$json_a)
    {
        if($json_a[heroic] == 1)
        {
            return "true";
        }

        return "false";
    }

    private function parseDrop(&$json_a)
    {
        $drop = "Unknown";

        if($json_a[sourcemore][0][n] != NULL)
        {
            $drop = $json_a[sourcemore][0][n];
        }

        return $drop;
    }

    private function parseSlot(&$xmlDoc)
    {
        return $xmlDoc->item->inventorySlot['id'];
    }
}

function localURL($wowid)
{
    return "../data/" . $wowid . ".xml";
}

function wowheadURL($wowid)
{
    $wowurlhead = "http://www.wowhead.com/item=";
    $wowurltail = "&xml";

    return $wowurlhead . $wowid . $wowurltail;
}

function tojson(&$xmlDoc)
{
    $jsonstr = '{' . $xmlDoc->item->json . '}';
    $json_a = json_decode($jsonstr, true);

    return $json_a;
}
?>